﻿package com.shootingGame.weaponsClasses.launchProfiles
{
	import com.common.utils.Math2;
	import com.shootingGame.GamePlayManager;
	import com.shootingGame.weaponsClasses.Weapon;
	import com.shootingGame.weaponsClasses.WeaponInfo;
	import com.shootingGame.weaponsClasses.ordinance.MachineGunOrdinance;
	import com.shootingGame.weaponsClasses.ordinance.Ordinance;
	import com.shootingGame.weaponsClasses.ordinance.RocketOrdinance;
	import com.shootingGame.weaponsClasses.ordinance.SimpleBulletOrdinance;
	
	import flash.geom.Point;
	
	public class LaunchProfile
	{
		public static const LINEAR_TRAJECTORY : String = "lineartrajectory"; //bullet
		public static const ARC_TRAJECTORY : String = "arctrajectory"; // grenade
		public static const ADJUSTING_TRAJECTORY : String = "adjustingtrajectory"; //guided, homing
		
		private var sbo : SimpleBulletOrdinance;
		private var mgo : MachineGunOrdinance;
		private var rbo : RocketOrdinance;
		//public static const SIMPLE_BULLET : String = "com.shootingGame.weaponsClasses.ordinance.simplebullet";
		
		public function LaunchProfile()
		{
			
		}
		
		//Returns array of launched ordinance
		public static function launch(launchProfile : String, weapon : Weapon, weaponInfo : WeaponInfo, targetX : Number, targetY : Number) : Array{
			var ordinanceList:Array;
			var ordinanceRotation:Number;
			var ordinance : Ordinance;
			var ordSpawnPoint_Local:Point;
			var ordSpawnPoint_Global:Point;
			
			if(weaponInfo.shotsFired()){
				switch(launchProfile){
					case LINEAR_TRAJECTORY :
						ordinanceList = new Array();
						ordSpawnPoint_Global = weapon.model.localToGlobal(new Point(weapon.model.nozzle.x, weapon.model.nozzle.y));
						ordSpawnPoint_Local = GamePlayManager.instance.ordinanceHolder.globalToLocal(ordSpawnPoint_Global);
						//ordinanceRotation = Player(weapon.attachedTo).facing;
						ordinanceRotation = Math2.getAngleOfElevationDegree(ordSpawnPoint_Global.x, ordSpawnPoint_Global.y, targetX, targetY);
						ordinance = weaponInfo.ordinanceSample.clone();
						ordinance.memoryX = ordSpawnPoint_Local.x;
						ordinance.memoryY = ordSpawnPoint_Local.y;
						ordinance.source = ordSpawnPoint_Local;
						ordinance.target = GamePlayManager.instance.ordinanceHolder.globalToLocal(new Point(targetX, targetY));
						ordinance.facing = ordinanceRotation;
						ordinance.velocityDirection = ordinanceRotation;
						ordinance.speed = weaponInfo.ordinanceSpeed;
						ordinanceList.push(ordinance);
						break;
					
					case ARC_TRAJECTORY :
//						ordSpawnPoint = weapon.model.localToGlobal(new Point(weapon.model.nozzle.x, weapon.model.nozzle.y));
//						ordinanceRotation = Math2.getRelativeElevationAngleDegree(ordSpawnPoint.x, ordSpawnPoint.y, targetX, targetY);
//						ordinance = new classRef(weapon.attachedTo, launchProfile, weaponInfo.damage, ordSpawnPoint.x, ordSpawnPoint.y, targetX, targetY, ordinanceRotation, 1);
//						ordinanceList.push(ordinance);
						break;

					case ADJUSTING_TRAJECTORY :
						ordinanceList = new Array();
						ordSpawnPoint_Global = weapon.model.localToGlobal(new Point(weapon.model.nozzle.x, weapon.model.nozzle.y));
						ordSpawnPoint_Local = GamePlayManager.instance.ordinanceHolder.globalToLocal(ordSpawnPoint_Global);
						//ordinanceRotation = Player(weapon.attachedTo).facing;
						ordinanceRotation = Math2.getAngleOfElevationDegree(ordSpawnPoint_Global.x, ordSpawnPoint_Global.y, targetX, targetY);
						ordinance = weaponInfo.ordinanceSample.clone();
						ordinance.memoryX = ordSpawnPoint_Local.x;
						ordinance.memoryY = ordSpawnPoint_Local.y;
						ordinance.source = ordSpawnPoint_Local;
						ordinance.target = GamePlayManager.instance.ordinanceHolder.globalToLocal(new Point(targetX, targetY));
						ordinance.facing = ordinanceRotation;
						ordinance.velocityDirection = ordinanceRotation;
						ordinance.speed = weaponInfo.ordinanceSpeed;
						ordinanceList.push(ordinance);
						break;
				}
			}
			return ordinanceList;
		}
		
		public static function updateTrajectory(launchProfile : String, ordinance : Ordinance, sourcePoint : Point, targetPoint : Point){
			//INEVITABLE CHANGE MARKER
			//The following line(s) of code will need to be changed in future.
			//Reason: SCROLLING MAP and memory storage of non-rendered off-screen objects (essential server function).
			//var ordPosition_Local : Point = GamePlayManager.instance.bulletHolder.localToGlobal(new Point(ordinance.memoryX, ordinance.memoryY));
			var weaponInfo : WeaponInfo = ordinance.weaponInfo;
			if(launchProfile == LINEAR_TRAJECTORY)
				return;
			if(launchProfile == ARC_TRAJECTORY){
				
				return;
			}
			if(launchProfile == ADJUSTING_TRAJECTORY){
				if(weaponInfo.turningRate > 0){
//					trace(targetX, targetY, ordSpawnPoint_Global);
					//var desiredFacing : Number = Math2.getAngleOfElevationDegree(ordPosition_Local.x, ordPosition_Local.y, targetPoint_Local.x, targetPoint_Local.y);
//					trace(sourcePoint, targetPoint);
					var desiredFacing : Number = Math2.getAngleOfElevationDegree(sourcePoint.x, sourcePoint.y, targetPoint.x, targetPoint.y);
					var deltaAngleRequired : Number = Math2.getChangeInAngle(desiredFacing, Math2.convertTo180DegreeAngle(ordinance.facing));
					var deltaAngle : Number;
					if(Math.abs(deltaAngleRequired) <= weaponInfo.turningRate)
						deltaAngle = deltaAngleRequired;
					else{
						if(deltaAngleRequired > 0)
							deltaAngle = weaponInfo.turningRate;
						else
							if(deltaAngleRequired < 0)
								deltaAngle = -weaponInfo.turningRate;
					}
					ordinance.facing += deltaAngle;
					ordinance.velocityDirection = ordinance.facing;
				}
				return;
			}
		} 

	}
}
